<template>
  <Dialog v-model="dialogVisible" title="督导检查审核" width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="检查记录">
        <el-card class="record-info">
          <div class="info-row">
            <span class="label">检查时间：</span>
            <span>{{ supervisionInfo.checkTime }}</span>
          </div>
          <div class="info-row">
            <span class="label">检查人员：</span>
            <span>{{ supervisionInfo.checkerName }}</span>
          </div>
          <div class="info-row">
            <span class="label">检查内容：</span>
            <span>{{ supervisionInfo.checkContent }}</span>
          </div>
          <div class="info-row">
            <span class="label">发现问题：</span>
            <span>{{ supervisionInfo.foundIssues }}</span>
          </div>
        </el-card>
      </el-form-item>
      
      <el-form-item label="审核结果" prop="auditResult">
        <el-radio-group v-model="formData.auditResult">
          <el-radio label="通过">审核通过</el-radio>
          <el-radio label="不通过">审核不通过</el-radio>
          <el-radio label="需要整改">需要整改</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="审核意见" prop="auditComments">
        <el-input
          v-model="formData.auditComments"
          type="textarea"
          :rows="4"
          placeholder="请输入审核意见"
        />
      </el-form-item>
      
      <el-form-item label="整改要求" v-if="formData.auditResult === '需要整改'">
        <el-input
          v-model="formData.rectificationRequirements"
          type="textarea"
          :rows="3"
          placeholder="请输入具体的整改要求"
        />
      </el-form-item>
      
      <el-form-item label="整改期限" v-if="formData.auditResult === '需要整改'">
        <el-date-picker
          v-model="formData.rectificationDeadline"
          type="date"
          placeholder="请选择整改期限"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="后续跟进">
        <el-checkbox-group v-model="formData.followUpActions">
          <el-checkbox label="需要复查">需要复查</el-checkbox>
          <el-checkbox label="通报相关部门">通报相关部门</el-checkbox>
          <el-checkbox label="纳入考核">纳入考核</el-checkbox>
          <el-checkbox label="建立档案">建立档案</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      
      <el-form-item label="审核附件">
        <el-upload
          v-model:file-list="formData.attachments"
          action="#"
          :auto-upload="false"
          multiple
        >
          <el-button type="primary">选择文件</el-button>
          <template #tip>
            <div class="el-upload__tip">支持上传jpg/png/pdf文件，且不超过10MB</div>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">提交审核</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

defineOptions({ name: 'SupervisionAudit' })

const emit = defineEmits(['success'])

const dialogVisible = ref(false)
const formLoading = ref(false)
const formRef = ref()

const supervisionInfo = reactive({
  id: undefined,
  checkTime: '',
  checkerName: '',
  checkContent: '',
  foundIssues: ''
})

const formData = reactive({
  auditResult: '',
  auditComments: '',
  rectificationRequirements: '',
  rectificationDeadline: null,
  followUpActions: [],
  attachments: []
})

const formRules = reactive({
  auditResult: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
  auditComments: [{ required: true, message: '请输入审核意见', trigger: 'blur' }]
})

/** 打开弹窗 */
const open = async (supervision: any) => {
  dialogVisible.value = true
  Object.assign(supervisionInfo, supervision)
  resetForm()
}

/** 重置表单 */
const resetForm = () => {
  formData.auditResult = ''
  formData.auditComments = ''
  formData.rectificationRequirements = ''
  formData.rectificationDeadline = null
  formData.followUpActions = []
  formData.attachments = []
  formRef.value?.resetFields()
}

/** 提交表单 */
const submitForm = async () => {
  if (!formRef.value) return
  
  const valid = await formRef.value.validate()
  if (!valid) return
  
  try {
    formLoading.value = true
    
    const auditData = {
      id: supervisionInfo.id,
      auditResult: formData.auditResult,
      auditComments: formData.auditComments,
      rectificationRequirements: formData.rectificationRequirements,
      rectificationDeadline: formData.rectificationDeadline,
      followUpActions: formData.followUpActions.join(','),
      auditStatus: formData.auditResult === '通过' ? 2 : formData.auditResult === '不通过' ? 3 : 4
    }
    
    // TODO: 调用审核督导检查的API
    // await auditSupervision(auditData)
    
    ElMessage.success('督导检查审核成功')
    dialogVisible.value = false
    emit('success')
  } catch (error) {
    console.error('督导检查审核失败:', error)
    ElMessage.error('督导检查审核失败')
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open })
</script>

<style scoped>
.record-info {
  background-color: #f5f7fa;
  border: 1px solid #e4e7ed;
}

.info-row {
  display: flex;
  margin-bottom: 8px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.label {
  font-weight: bold;
  min-width: 80px;
  color: #606266;
}
</style>
